package com.tongjie.hotelbooking.dto;

import com.tongjie.hotelbooking.im.conversation.entiry.MemberConversation;
import com.tongjie.hotelbooking.im.message.entiry.ImMessage;
import com.tongjie.hotelbooking.order.entity.Order;
import com.tongjie.hotelbooking.order.entity.RoomInfo;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.ArrayList;

public class DtoConverters {

    public static OrderDto orderDtoConverter(Order order) {
        return OrderDto.builder()
                .id(order.getId())
                .orderNumber(order.getOrderNumber())
                .hotelName(order.getHotelName())
                .plannedCheckInDate(DateFormatUtils.format(order.getPlannedCheckInDate(), "yyyy-MM-dd"))
                .plannedCheckOutDate(DateFormatUtils.format(order.getPlannedCheckOutDate(), "yyyy-MM-dd"))
                .orderStatus(order.getOrderStatus())
                .allowedHousemate(order.getAllowedHousemate())
                .createTime(DateFormatUtils.format(order.getCreateTime(), "yyyy-MM-dd"))
                .dayCount(order.getDayCount())
                .needHotelInvoice(order.getNeedHotelInvoice())
                .needBreakfast(order.getNeedBreakfast())
                .creatorId(order.getCreateUser().getId())
                .creatorNickName(order.getCreateUser().getNickName())
                .build();
    }

    public static OrderDto orderDtoDetailConverter(Order order) {
        OrderDto dto = OrderDto.builder()
                .id(order.getId())
                .orderNumber(order.getOrderNumber())
                .orderType(order.getOrderType())
                .region(order.getRegion())
                .hotelName(order.getHotelName())
                .plannedCheckInDate(DateFormatUtils.format(order.getPlannedCheckInDate(), "yyyy-MM-dd"))
                .plannedCheckOutDate(DateFormatUtils.format(order.getPlannedCheckOutDate(), "yyyy-MM-dd"))
                .dayCount(order.getDayCount())
                .singleRoomBudget(order.getSingleRoomBudget())
                .totalPeopleCount(order.getTotalPeopleCount())
                .kidCount(order.getKidCount())
                .needBreakfast(order.getNeedBreakfast())
                .noSmoking(order.getNoSmokingBreakfast())
                .arriveTime(DateFormatUtils.format(order.getArriveTime(), "HH:mm"))
                .additionalBudget(order.getAdditionalBudget())
                .needHotelInvoice(order.getNeedHotelInvoice())
                .totalBudget(order.getTotalBudget())
                .allowedHousemate(order.getAllowedHousemate())
                .orderStatus(order.getOrderStatus())
                .creatorId(order.getCreateUser().getId())
                .creatorNickName(order.getCreateUser().getNickName())
                .createTime(DateFormatUtils.format(order.getCreateTime(), "yyyy-MM-dd"))
                .roomInfos(new ArrayList<>())
                .build();

        for (RoomInfo roomInfo : order.getRoomInfos()) {
            RoomInfo roomInfo1 = RoomInfo.builder().id(roomInfo.getId()).roomCount(roomInfo.getRoomCount()).roomType(roomInfo.getRoomType()).build();
            dto.getRoomInfos().add(roomInfo1);
        }
        return dto;
    }

    public static ImMessageDto imMessageToMessageDtoConverter(ImMessage imMessage) {
        return ImMessageDto.builder()
                .id(imMessage.getId())
                .conversationId(imMessage.getConversation().getId())
                .senderId(imMessage.getSenderUser().getId())
                .avatar(imMessage.getSenderUser().getAvatar())
                .messageType(imMessage.getMessageType())
                .messageBody(imMessage.getTextMessage())
                .sendDate(DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT.format(imMessage.getSendTime()))
                .build();
    }

    public static MemberConversationDto memberConversationToMemberConversationDto(MemberConversation memberConversation) {
        return MemberConversationDto.builder()
                .id(memberConversation.getId())
                .conversationId(memberConversation.getConversation().getId())
                .memberUserId(memberConversation.getMemberUser().getId())
                .memberConversationName(memberConversation.getMemberConversationName())
                .redDot(memberConversation.getRedDot())
                .avatar(memberConversation.getAvatar())
                .build();
    }
}
